/**
 * This file is part of the ePNK project (Petri Net Kernel for eclipse). The ePNK
 * is platform for Petri net tools based on the PNML transfer format. Its
 * main idea is to provide generic Petri net types which can be easily plugged
 * in and a simple generic GMF editor, that can be used for graphically editing
 * nets of any plugged in type. Additional functionality can be plugged in.
 * 
 * Note that this project is still in an experimental phase, and is also used
 * as a case study for EMF/GMF based Model-based Software Engineering.
 * 
 * Copyright (C) 2010 Ekkart Kindler, eki@imm.dtu.dk
 * The license under which this software will be distributed is still to be decided -
 * it will probably be the EPL.
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.ginet.diagram.part;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.pnml.tools.epnk.pnmlcoremodel.PetriNet;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractGINet;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.generation.abstract2concrete.AbstractGIM2ConcreteGIM;

public class GenerateCGIMAction implements IObjectActionDelegate {
	
	private PetriNet ginet; 
	
	private Page abstractGIModule;
	
	private Shell shell;


//	public boolean isEnabled(PetriNet petrinet) {
//		if (petrinet != null) {
//			PetriNetType type = petrinet.getType();
//			return type != null && type instanceof GINet;
//		}
//		return false;
//	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	final public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
	
	
	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	final public void selectionChanged(IAction action, ISelection selection) {
		ginet = null;
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.size() == 1
					&& structuredSelection.getFirstElement() instanceof ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page) {
//				((Page)structuredSelection.getFirstElement()).eContainer();
				abstractGIModule = (Page)structuredSelection.getFirstElement();
				ginet = (PetriNet)abstractGIModule.eContainer();
			}
		}
//		action.setEnabled(isEnabled(petrinet));
	}
	
	
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	final public void run(IAction action) {
		
//	    Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().

		AbstractGINet abstractPN = (AbstractGINet) abstractGIModule.eContainer();
		
		AbstractGIM2ConcreteGIM agim2cgim = new AbstractGIM2ConcreteGIM(abstractPN);
		
		// Creates and adds a concrete GI module to the GI-Net based on an abstract GI module
		agim2cgim.addConcreteGIModule(abstractGIModule);
		
		// Get the resource path 
		String path = abstractGIModule.eResource().getURI().path();
		
		// Get the file name
		String fileName = abstractGIModule.eResource().getURI().lastSegment();
		
		// The full path to the new PNML file
		String newFileName = path.substring(0, path.indexOf(fileName)) + "cgim.pnml";

		// Saves the net to a pnml file
		agim2cgim.createPNMLFile(newFileName);
		
		MessageDialog.openInformation(
				shell,
				"Transformation",
				"Page created.");	
//				"Generate GI-Net was executed.");	
		
//		AbstractEPNKJob job = createJob(petrinet,defaultInput);
//		
//		if (job.prepare()) {
//			defaultInput = job.getInput();
//			// eki: Since there are porblems with user jobs, I do not
//			// start the job as a user job right now.
//			// job.setUser(true);
//			job.setPriority(Job.SHORT);
//			job.schedule(); 
//		}
	}

	

}
